<template>
  <a-card :bordered="false" class="card-area ccslh">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item label="标题">
              <a-input v-model="queryParam.ggbt"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="用户端">
              <a-select :allowClear="true" v-model="queryParam.gglxm">
                <a-select-option v-for="i in TYPE0508" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="16" :sm="24">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button type="primary" @click="batchDelete">批量删除</a-button>
              <a-button type="primary" @click="openAdd">添加公告</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <notice-edit
      ref="noticeEdit"
      :visible.sync="visible"
      :editId.sync="editId"
      :TYPE0508.sync="TYPE0508"
      @cancel="visible = false"
      @create="handleCreate"
    />
    <s-table
      ref="table"
      size="default"
      rowKey="id"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      :pageSize="parameter.pageSize"
      >
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
      </span>
      <span slot="gglxm" slot-scope="text">
        {{ TYPE0508.find(i => i.code == text).name }}
      </span>
      <a slot="operation" slot-scope="a,b" @click="openEdit(b)">修改</a>
    </s-table>
  </a-card>
</template>

<script>
import { STable } from '~'
import noticeEdit from './noticeEdit'
import { mapState } from 'vuex'

export default {
  name: 'TableList',
  components: {
    STable,
    noticeEdit
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      visible: false,
      editId: null,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '序号',
          width: 80,
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '标题',
          width: 250,
          dataIndex: 'ggbt',
        },
        {
          title: '用户端',
          dataIndex: 'gglxm',
          width: 120,
          scopedSlots: { customRender: 'gglxm' }
        },
        {
          title: '公告内容',
          dataIndex: 'ggnr',
          width: 400
        },
        {
          title: '操作',
          width: 120,
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.home.noticeList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true, // 是否显示alert，已选中
      confirmLoading: false
    }
  },
  computed: {
    ...mapState({
      TYPE0508: state => state.app['TYPE0508']
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0508')
  },
  methods: {
    handleCreate () {
      const form = this.$refs.noticeEdit.form
      form.validateFields((err, values) => {
        if (err) {
          return
        }
        let baseApi = this.editId ? 'noticeUpdate' : 'noticeAdd'
        values.ggnr = this.$refs.noticeEdit.ggnr
        this.$api.home[baseApi](Object.assign({id: this.editId}, values)).then(res => {
          this.$message.success(res.msg)
          this.$refs.table.refresh(false)
        })
        form.resetFields()
        this.visible = false
      })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    openAdd () {
      this.editId = null
      this.visible = true
      this.$refs.noticeEdit.form.resetFields()
    },
    openEdit (notice) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.noticeEdit.form.setFieldsValue(notice)
        this.$refs.noticeEdit.ggnr = notice.ggnr
        this.editId = notice.id
      })
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的公告信息')
        return
      }
      this.$confirm({
        title: '确定删除所选中的公告信息?',
        content: '当您点击确定按钮后，这些记录将会被彻底删除',
        centered: true,
        onOk: () => {
          let campusIds = []
          this.$api.home.noticeDelete(this.selectedRowKeys.join(',')).then(res => {
            this.$message.success(res.msg)
            this.selectedRowKeys = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
    }
  }
}
</script>
<style lang="less">
.ccslh{
  table {
    table-layout:fixed;
  }
  tr td:nth-child(4){
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

</style>
